package com.my.meitianbackup.crawler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;

import com.alibaba.fastjson.JSON;
import com.jfinal.plugin.activerecord.Db;
import com.my.meitianbackup.common.Constants;
import com.my.meitianbackup.model.Mogujie;
import com.my.meitianbackup.model.MogujieImgs;
import com.my.meitianbackup.utils.HttpUtils;

public class MogujieCrawler {

	public static void main(String[] args) {
		getMogujieList();
	}
	public static HashMap<String, Integer> getMogujieList() {
		HashMap<String,Integer> countMap = new HashMap<String,Integer>();
		
		for(int i=0; i < Constants.moguCateId.length; i++) {
			String listPath = "http://union.mogujie.com/jsonp/proms/1";
			Map<String, String> map = new HashMap<String, String>();
			Integer index = 0;
			for(int j=1; j<=1; j++) {
				map.put("pageNo", j+"");
				map.put("pageSize", "48");
				//map.put("selectionTags", "%E7%B2%BE%E9%80%89");
				map.put("cid",Constants.moguCateId[i]);
				map.put("getAll","true");
				map.put("sort","NEW");
				String listRes = HttpUtils.startGet(listPath, map);
				listRes = listRes.substring(5,listRes.length()-1);
				JSONObject jb = new JSONObject(listRes);
				System.out.println("kb:"+jb);
				JSONArray ja = jb.getJSONObject("data").getJSONArray("moduleList").getJSONObject(0).getJSONArray("items");
				if(ja.length()==0)
					continue;
				
				for(int k=0; k<ja.length(); k++) {
					JSONObject json = ja.getJSONObject(k);
					Mogujie mgj = JSON.parseObject(json.toString(), Mogujie.class);
					if(!(mgj.getImage().contains("640x960") || mgj.getImage().contains("340x510"))) {
						continue;
					}
					String detailPath = "http://union.mogujie.com/jsonp/unionTools/1";
					map.clear();
					map.put("uid", mgj.getTradeItemEsc());
					map.put("promid", mgj.getCampaignEsc());
					map.put("itemLink", "http://shop.mogujie.com/detail/"+mgj.getTradeItemEsc());
					String detailRes = HttpUtils.startGet(detailPath, map);
					detailRes = detailRes.substring(5,detailRes.length()-1);
					JSONObject detail = new JSONObject(detailRes).getJSONObject("data");
					mgj.setCpsLink(detail.getString("cpsLink"));
					mgj.setItemPrice(detail.getInt("itemPrice"));
					mgj.setCid(Constants.moguCateId[i]);
					mgj.setCate(Constants.moguCate[i]);
					mgj.setCateEn(Constants.moguCateEn[i]);
					System.out.println(mgj.toJson());
					try {
						mgj.save();
						index++;
					} catch (Exception e) {
						mgj.update();
						continue;
					}
					JSONArray imgs = detail.getJSONArray("picLinks");
					List<MogujieImgs> imgList = new ArrayList();
					for(int n=0; n<imgs.length(); n++) {
						String imgUrl = imgs.getString(n);
						if(!imgUrl.contains("640x960"))
							continue;
						MogujieImgs mgjImg = new MogujieImgs();
						mgjImg.setNumiid(mgj.getId());
						mgjImg.setImgUrl(imgs.getString(n));
						imgList.add(mgjImg);
					}
					if(imgList.size()>=2) {
						int[] aa = Db.batchSave(imgList, imgList.size());
						System.out.println(Arrays.toString(aa));
					} else {
						mgj.delete();
						index--;
					}
				}
			}
			countMap.put(Constants.moguCateEn[i], index);
		}
		
		return countMap;
	}
}
